
import 'package:qr_flutter/qr_flutter.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/generated/assets.dart';
import 'package:safetymonitor/global.dart';
import 'package:safetymonitor/modules/home/user/version_info/update_util.dart';
import 'package:safetymonitor/modules/home/user/version_info/version_update_controller.dart';
import 'package:url_launcher/url_launcher_string.dart';

/// 新版本更新页面
class VersionUpdatePage extends StatelessWidget {
  VersionUpdatePage({super.key});

  final controller = Get.put(VersionUpdateController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: const Color(0xFFFFFFFF),
            appBar: CustomAppBar(titleText: '版本更新'),
            body: Column(
              children: [
                QrImageView(
                  data: UpdateUtil.downloadAddress,
                  version: QrVersions.auto,
                  padding: const EdgeInsets.symmetric(vertical: 40,horizontal: 45),
                ),
                Text('浏览器扫描这个二维码,可手动下载最新版',style: TextStyle(fontSize: 14.sp, color: const Color(0xff999999)),),
                AppGaps.vGap16,
                Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        _getTextView(title: '${'当前版本号'}:', value: 'v${Global.packageInfo.version}'),
                        _getTextView(
                          title: '${'检查更新'}:',
                          value: controller.hasNewVersion ? '${'发现新版本'} v${controller.appUpgradeEntity?.appVersion}' : '当前已是最新版本',
                          color: controller.hasNewVersion ? const Color(0xFF2267C0) : null,
                        ).clickOnce(controller.checkUpdate),
                        _getUpdateContent(),
                      ],
                    ),
                  ).paddingSymmetric(horizontal: 20.w),
                ),
                Visibility(
                  visible: controller.hasNewVersion,
                  child: Container(
                      height: 40.w,
                      alignment: Alignment.center,
                      margin: EdgeInsets.symmetric(horizontal: 16.w),
                      decoration: BoxDecoration(
                        color: const Color(0xff2267C0),
                        borderRadius: BorderRadius.all(Radius.circular(20.w)),
                      ),
                      child: Text(
                        '点击升级',
                        style: TextStyle(fontSize: 16.sp, color: const Color(0xffffffff)),
                      )).clickOnce(controller.upgrade),
                ),
                AppGaps.vGap30,
              ],
            ),
          );
        },
    );
  }

  /// 获取更新内容显示
  Widget _getUpdateContent() {
    return Visibility(
      visible: controller.hasNewVersion,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '${'更新内容'}:',
            style: TextStyle(
              fontSize: 16.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xff071D56),
            ),
          ),
          AppGaps.vGap10,
          Text(
            controller.appUpgradeEntity?.upgradeDescription ?? '',
            style: TextStyle(
              fontSize: 16.sp,
              height: 1.2,
              fontWeight: FontWeight.w400,
              color: const Color(0xff071D56),
            ),
          ),
          AppGaps.vGap20,
          Divider(height: 0.5.w, color: const Color(0xffEAF2FA)),
        ],
      ).marginSymmetric(vertical: 20.w),
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value, Color? color}) {
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 16.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff071D56),
              ),
            ),
            AppGaps.hGap10,
            Expanded(
              child: Container(
                alignment: Alignment.centerRight,
                child: Text(
                  value,
                  style: TextStyle(
                    fontSize: 16.sp,
                    height: 1.2,
                    fontWeight: FontWeight.w400,
                    color: color ?? const Color(0xff071D56),
                  ),
                ),
              ),
            ),
          ],
        ).marginSymmetric(vertical: 20.w),
        Divider(height: 0.5.w, color: const Color(0xffEAF2FA)),
      ],
    );
  }
}
